import 'package:flutter/material.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'state.dart';
import 'address_model.dart';

class AddressSelectorLogic extends GetxController {
  final state = AddressSelectorState();

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();

    requestGetProvinceInfo();
    //datas第一层级初始化赋值

  }

  void requestGetProvinceInfo() {
    var cityParams = {
      "access_token": SpUtil.getString('token'),
    };
    Api.getProvinceInfo(cityParams).listen((event) {
      if (event['code'] == 0) {
        if (event["prv_list"] != null) {
          AddressInfoModel addressInformation = AddressInfoModel.fromJson(event);
          List<AddressItemModel> addressArr = [];
          addressInformation.prv_list.forEach((item) {
            addressArr.add(AddressItemModel(addressId: item.prv_num_id, addressName: item.prv_name));
          });
          // List<AddressItemModel> arr = [];
          // event['prv_list'].forEach((item) {
          //   arr.add(AddressItemModel(addressId: item["prv_num_id"], addressName: "prv_name"));
          // });

          state.addressProvinceList = addressArr;
        } else {
          state.addressProvinceList = [];
        }
      }
      update();
    });
  }

  void requestGetCityInfo(String prvNumId) {
    var cityParams = {
      "access_token": SpUtil.getString('token'),
      "prv_num_id": prvNumId,
    };
    Api.getCityInfo(cityParams).listen((event) {
      if (event['code'] == 0) {
        AddressInfoModel addressInformation = AddressInfoModel.fromJson(event);
        List<AddressItemModel> addressArr = [];
        addressInformation.city_list.forEach((item) {
          addressArr.add(AddressItemModel(addressId: item.city_num_id, addressName: item.city_name));
        });
        state.addressCityList = addressArr;
      }
      update();
    });
  }

  void requestGetCityAreaInfo(String cityNumId) {
    var cityParams = {
      "access_token": SpUtil.getString('token'),
      "city_num_id": cityNumId,
    };
    Api.getCityAreaInfo(cityParams).listen((event) {
      if (event['code'] == 0) {
        AddressInfoModel addressInformation = AddressInfoModel.fromJson(event);
        List<AddressItemModel> addressArr = [];
        addressInformation.city_area_list.forEach((item) {
          addressArr.add(AddressItemModel(addressId: item.city_area_num_id, addressName: item.city_area_name));
        });
        state.addressCityAreaList = addressArr;
      }
      update();
    });
  }

  void requestGetTownInfo(String cityAreaNumId) {
    var cityParams = {
      "access_token": SpUtil.getString('token'),
      "city_area_num_id": cityAreaNumId,
    };
    Api.getTownInfo(cityParams).listen((event) {
      if (event['code'] == 0) {
        AddressInfoModel addressInformation = AddressInfoModel.fromJson(event);
        List<AddressItemModel> addressArr = [];
        addressInformation.town_list.forEach((item) {
          addressArr.add(AddressItemModel(addressId: item.town_num_id, addressName: item.town_name));
        });
        state.addressTownList = addressArr;
      }
      update();
    });
  }
}
